package com.yeraphael.service.impl;

import com.yeraphael.entity.ReportJobData;
import com.yeraphael.entity.EmpTempData;
import com.yeraphael.entity.ReportMultiData;
import com.yeraphael.entity.ReportStudentData;
import com.yeraphael.mapper.ReportMapper;
import com.yeraphael.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public List<ReportMultiData> getGerder() {
        List<ReportMultiData> gd = reportMapper.getGerder();

        gd.forEach(item -> {
            if ("1".equals(item.getName())) {
                item.setName("男性");
            } else if ("2".equals(item.getName())) {
                item.setName("女性");
            } else {
                item.setName("未知");
            }
        });
        return gd;
    }



    @Override
    public ReportJobData getJobData() {
        List<EmpTempData> rows = reportMapper.getjobData();
//        职位, 1 班主任, 2 讲师 , 3 学工主管, 4 教研主管, 5 咨询师
        ReportJobData jobData = new ReportJobData();

        if( rows != null && !rows.isEmpty()){
            String[] jobList = new String[rows.size()];
            Integer[] dataList = new Integer[rows.size()];

            for (int i = 0; i < rows.size(); i++) {
                EmpTempData row = rows.get(i);
                String job = row.getName();
                String jobName;

//                Cannot invoke "String.hashCode()" because "<local8>" is null
//                你的代码里某个地方把一个 null 的字符串 当成 Map 的 key、或者当成需要 .equals() / .hashCode() 的对象了。
//
//                Java 在做 HashMap key 存储、switch-case（字符串）、equals()、甚至 toString() 的时候，底层都会调用 String.hashCode()。
//                if (job == null) {
//                    jobName = "暂无";
//                } else {
//                    switch (job) {
//                        case "1": jobName = "班主任"; break;
//                        case "2": jobName = "讲师"; break;
//                        case "3": jobName = "学工主管"; break;
//                        case "4": jobName = "教研主任"; break;
//                        case "5": jobName = "咨询师"; break;
//                        default: jobName = "暂无";
//                    }

                if ("1".equals(job)) {
                    jobName = "班主任";
                } else if ("2".equals(job)) {
                    jobName = "讲师";
                } else if ("3".equals(job)) {
                    jobName = "学工主管";
                } else if ("4".equals(job)) {
                    jobName = "教研主任";
                } else if ("5".equals(job)) {
                    jobName = "咨询师";
                } else {
                    jobName = "暂无";
                }

                jobList[i] = jobName;
                dataList[i] = row.getValue();
            }
            jobData.setJobList(jobList);
            jobData.setDataList(dataList);
        }

        return jobData;
    }

    @Override
    public List<ReportMultiData> getDegreeData() {
        List<ReportMultiData> gd = reportMapper.getDegreeData();
//        '最高学历, 1:初中, 2:高中, 3:大专, 4:本科, 5:硕士, 6:博士'
        gd.forEach(item -> {
            if ("1".equals(item.getName())) {
                item.setName("初中");
            } else if ("2".equals(item.getName())) {
                item.setName("高中");
            } else if ("3".equals(item.getName())) {
                item.setName("大专");
            } else if ("4".equals(item.getName())) {
                item.setName("本科");
            } else if ("5".equals(item.getName())) {
                item.setName("硕士");
            } else {
                item.setName("博士");
            }
        });
        return gd;
    }

    @Override
    public ReportStudentData getCountData() {
        List<EmpTempData> rows = reportMapper.getCountData();
        ReportStudentData studentData = new ReportStudentData();

        if( rows != null && !rows.isEmpty()){
            String[] clazzList = new String[rows.size()];
            Integer[] dataList = new Integer[rows.size()];

            for (int i = 0; i < rows.size(); i++) {
                EmpTempData row = rows.get(i);
                clazzList[i] = row.getName();
                dataList[i] = row.getValue();
            }
            studentData.setClazzList(clazzList);
            studentData.setDataList(dataList);
        }

        return studentData;
    }
}
